package tree;

/**
 * Created with IntelliJ IDEA.
 * Description: 二叉树代码测试
 * User: 王圆豪
 * Date: 2024-08-23
 * Time: 19:02
 */
public class Tree {
    int val;
    Tree left;
    Tree right;
    Tree() {}
    Tree(int val) { this.val = val; }

    Tree(int val, Tree left, Tree right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }


    public static void main(String[] args) {
        Tree tree = new Tree();
        tree.val = 0;
        tree.left = new Tree(1);
        tree.right = new Tree(2);
        System.out.println(zNum(tree, 1));
    }

    public static int size(Tree root) {
        return root == null ? 0 : size(root.left) + size(root.right) + 1;
    }

    public static int num(Tree root) {
        if (root == null) {
            return 0;
        }
        return root.left == null && root.right == null ? 1 : num(root.left) + num(root.right);
    }

    public static int zNum(Tree root, int k) {
        if (root == null || k < 1) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return zNum(root.left, k - 1) + zNum(root.right, k - 1);
    }
}
